﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToursAgencyDAL;

namespace TourAgencyMVC.Controllers
{ 
    public class TestController : Controller
    {
        private ToursAgencyEntities db = new ToursAgencyEntities();

        //
        // GET: /Test/

        public ViewResult Index()
        {
            var tours = db.Tours.Include("Board").Include("Destination").Include("Transport");
            return View(tours.ToList());
        }

        //
        // GET: /Test/Details/5

        public ViewResult Details(int id)
        {
            Tour tour = db.Tours.Single(t => t.ID == id);
            return View(tour);
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
            ViewBag.BoardID = new SelectList(db.Boards, "ID", "Name");
            ViewBag.DestinationID = new SelectList(db.Destinations, "ID", "Name");
            ViewBag.TransportID = new SelectList(db.Transports, "ID", "Name");
            return View();
        } 

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(Tour tour)
        {
            if (ModelState.IsValid)
            {
                db.Tours.AddObject(tour);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.BoardID = new SelectList(db.Boards, "ID", "Name", tour.BoardID);
            ViewBag.DestinationID = new SelectList(db.Destinations, "ID", "Name", tour.DestinationID);
            ViewBag.TransportID = new SelectList(db.Transports, "ID", "Name", tour.TransportID);
            return View(tour);
        }
        
        //
        // GET: /Test/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tour tour = db.Tours.Single(t => t.ID == id);
            ViewBag.BoardID = new SelectList(db.Boards, "ID", "Name", tour.BoardID);
            ViewBag.DestinationID = new SelectList(db.Destinations, "ID", "Name", tour.DestinationID);
            ViewBag.TransportID = new SelectList(db.Transports, "ID", "Name", tour.TransportID);
            return View(tour);
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(Tour tour)
        {
            if (ModelState.IsValid)
            {
                db.Tours.Attach(tour);
                db.ObjectStateManager.ChangeObjectState(tour, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BoardID = new SelectList(db.Boards, "ID", "Name", tour.BoardID);
            ViewBag.DestinationID = new SelectList(db.Destinations, "ID", "Name", tour.DestinationID);
            ViewBag.TransportID = new SelectList(db.Transports, "ID", "Name", tour.TransportID);
            return View(tour);
        }

        //
        // GET: /Test/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tour tour = db.Tours.Single(t => t.ID == id);
            return View(tour);
        }

        //
        // POST: /Test/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tour tour = db.Tours.Single(t => t.ID == id);
            db.Tours.DeleteObject(tour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}